package com.rc.saas.tenant.service.acct;

import com.rc.saas.tenant.common.utils.StringUtil;
import com.rc.saas.tenant.mapper.acct.AcctBillOrderMapper;
import com.rc.saas.tenant.mapper.acct.AcctRuleMapper;
import com.rc.saas.tenant.mapper.acct.AcctSubItemMapper;
import com.rc.saas.tenant.mapper.order.OrderInfoMapper;
import com.rc.saas.tenant.model.acct.AcctBillOrder;
import com.rc.saas.tenant.model.acct.AcctBillOrderExample;
import com.rc.saas.tenant.model.acct.AcctSubItem;
import com.rc.saas.tenant.model.acct.AcctSubItemExample;
import com.rc.saas.tenant.service.order.OrderInfoService;
import com.rc.saas.tenant.vo.acct.AcctRuleResultVo;
import com.rc.saas.tenant.vo.acct.AcctRuleVo;
import com.rc.saas.tenant.vo.order.AcctRuleOrderResult;
import groovy.lang.Binding;
import groovy.lang.GroovyShell;
import org.apache.commons.lang3.StringUtils;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

@Service
public class AcctSubItemServiceImpl implements AcctSubItemService {
    private static Logger logger = LogManager.getLogger(AcctSubItemServiceImpl.class);

    @Autowired
    private AcctSubItemMapper acctSubItemMapper;

    @Autowired
    private OrderInfoService orderInfoService;

    @Autowired
    private AcctRuleMapper acctRuleMapper;

    @Autowired
    private AcctBillOrderMapper acctBillOrderMapper;

    @Override
    public AcctSubItemMapper getMapper() {
        return acctSubItemMapper;
    }

    @Override
    public List<AcctSubItem> listAcctSubitem(Long billOrderId) throws ParseException {
        List<AcctSubItem> acctSubItems = new ArrayList<>();
        Binding binding = new Binding();
        AcctBillOrder acctBillOrder = acctBillOrderMapper.selectAcctBillOrder(billOrderId);
        binding.setVariable("平台优惠金额", acctBillOrder.getTotalDiscountMoney());
        binding.setVariable("平台实收金额", acctBillOrder.getPayAmount());
        binding.setVariable("平台电费", acctBillOrder.getTotalElecMoney());
        binding.setVariable("平台总金额", acctBillOrder.getTotalMoney());
        binding.setVariable("平台电量", acctBillOrder.getTotalPower());
        binding.setVariable("平台服务费", acctBillOrder.getTotalSeviceMoney());
        if (acctBillOrder.getConsultAmount() == null) {
            binding.setVariable("咨询订单金额", new BigDecimal(0));
        } else {
            binding.setVariable("咨询订单金额", acctBillOrder.getConsultAmount());
        }

        GroovyShell shell = new GroovyShell(binding);

        //获取结算规则子项
        if (acctBillOrder.getAcctRuleId() != null && !"".equals(acctBillOrder.getAcctRuleId())) {
            AcctSubItemExample example = new AcctSubItemExample();
            example.createCriteria().andAcctRuleIdEqualTo(acctBillOrder.getAcctRuleId());
            acctSubItems = acctSubItemMapper.selectByExample(example);

            if (acctSubItems != null && acctSubItems.size() > 0) {
                for (int i = 0; i < acctSubItems.size(); i++) {
                    AcctSubItem acctSubItem = acctSubItems.get(i);
                    //子项规则
                    int index = acctSubItem.getSubItem().indexOf("=");
                    BigDecimal amount = checkBigDecimal((BigDecimal) shell.evaluate(acctSubItem.getSubItem().substring(index + 1)));
                    acctSubItem.setSubItem(acctSubItem.getSubItem() + "=" + String.valueOf(amount));
                }
            }
        }
        return acctSubItems;
    }

    public BigDecimal checkBigDecimal(BigDecimal o) {
        return o == null ? BigDecimal.ZERO : o.setScale(2, BigDecimal.ROUND_HALF_UP);
    }
}

